<?php

class subject_model extends CI_Model 
{
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
	
	// Get Subject based on Subject ID
	function getSubjectByID( $subject_id )
	{
		$this->db->where('subject_id', $subject_id);
		$this->db->from('tbl_subject');
		return $this->db->get();
	}
	
	// Get Subjects based on array of Subject IDs
	function getSubjectsByID( $subject_id_array )
	{
		$this->db->where('subject_id', '0');
	
		foreach ($subject_id_array as $subject_id)
			$this->db->or_where('subject_id', $subject_id);
			
		$this->db->from('tbl_subject');
		$this->db->order_by('subject_name', 'asc');
		return $this->db->get();
	}
	
	// Get Subject based on name
	function getSubjectByName( $subject_name )
	{
		$this->db->where('subject_name', $subject_name);
		$this->db->from('tbl_subject');
		return $this->db->get();
	}
	
	// Delete subject based on Subject ID
	function deleteSubjectByID( $subject_id )
	{
		$delete_where_array = array('subject_id' => $subject_id );
	
		$this->db->delete('tbl_subject', $delete_where_array );
	}
	
	// Add subject to table
	function addSubject( $subject_name )
	{
		$data_array = array('subject_name'=>$subject_name);
		$this->db->insert('tbl_subject', $data_array);
	}
	
	// Updates subject based on subject_id
	function updateSubject( $subject_id, $data_array )
	{
		$update_where_array = array('subject_id' => $subject_id );
	
		$this->db->update('tbl_subject', $data_array, $update_where_array); 
	}
	
	// Search Subject Table
	function getSearchResults( $search_by='subject_name', $terms='ALL', $offset=0, $limit=20 )
	{
		// If we do not want all results
		if ($terms != 'ALL')
		{
			$term_array = explode('%20', $terms);
			foreach ($term_array as $word)
				$this->db->like($search_by, $word, 'after');
		}
		$this->db->from('tbl_subject');
		$this->db->order_by($search_by, 'asc');
		$this->db->limit($limit, $offset);
		return $this->db->get();
	}
	
	// Get Total Number of Results for Search
	function getSearchCount( $search_by='subject_name', $terms='ALL' )
	{
		if ($terms != 'ALL')
		{
			$term_array = explode('%20', $terms);
			foreach ($term_array as $key=>$word)
				$this->db->like($search_by, $word, 'after');
		}
		$this->db->from('tbl_subject');
		return $this->db->count_all_results();
	}
}

?>